package com.approval.approvalapp.index.dbsx.ptbl.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.PtblXqBean;
import com.approval.approvalapp.bean.SbclckBean;
import com.approval.approvalapp.index.dbsx.ptbl.adapter.PtblxqAdapter;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.base.SbclckActivity;
import com.approval.approvalapp.util.base.YwSjCkActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bumptech.glide.Glide;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

//普通办理详情
public class PtblXqDbActivity extends BaseActivity2 {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.commit_tv)
    TextView commitTv;
    @Bind(R.id.bottm_rl)
    RelativeLayout bottmRl;
    @Bind(R.id.color_civ)
    CircleImageView colorCiv;
    @Bind(R.id.sqr_tv)
    TextView sqrTv;
    @Bind(R.id.sbsx_tv)
    TextView sbsxTv;
    @Bind(R.id.sbsj_tv)
    TextView sbsjTv;
    @Bind(R.id.blsx_tv)
    TextView blsxTv;
    @Bind(R.id.lxdh_tv)
    TextView lxdhTv;
    @Bind(R.id.zjlx_tv)
    TextView zjlxTv;
    @Bind(R.id.zjhm_tv)
    TextView zjhmTv;
    @Bind(R.id.zk_tv)
    TextView zkTv;
    @Bind(R.id.zk_iv)
    ImageView zkIv;
    @Bind(R.id.zk_ll)
    LinearLayout zkLl;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.name_tv)
    TextView name_tv;
    @Bind(R.id.current_node_name_tv)
    TextView current_node_name_tv;
    @Bind(R.id.ywsjck_ll)
    LinearLayout xywsjck_ll;

    public String business_object_code;
    public String business_object_type;
    public String cid;
    public String current_node_id;
    public String item_id;
    public String bsnum;
    public String sbsx;
    public String blsx;
    public String sbsj;
    public String sqr;
    private boolean zk = false;//默认是隐藏
    private PtblxqAdapter ptblxqAdapter;
    private List<PtblXqBean.BlyjShowBean> blyj_show;
    public String current_node_name;
    private PtblXqBean ptblXqBean;
    public boolean is_sign;
    public static PtblXqDbActivity instance;
    public String from_id;
    public String data_id;
    public String nodeId;
    public String nodeName;
    public String condition;
    public String gateway;


    @Override
    public int getLayoutId() {
        instance = this;
        return R.layout.activity_ptbl_xq;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
    }

    @Override
    public void initView() {
        //下面几个是接口参数
        business_object_code = getIntent().getStringExtra("business_object_code");
        business_object_type = getIntent().getStringExtra("business_object_type");
        cid = getIntent().getStringExtra("cid");
        current_node_id = getIntent().getStringExtra("current_node_id");
        item_id = getIntent().getStringExtra("item_id");
        bsnum = getIntent().getStringExtra("bsnum");
        //申报事项
        sbsx = getIntent().getStringExtra("sbsx");
        //办理事项
        blsx = getIntent().getStringExtra("blsx");
        //申报时间
        sbsj = getIntent().getStringExtra("sbsj");
        //申请人姓名
        sqr = getIntent().getStringExtra("sqr");
        //当前状态
        current_node_name = getIntent().getStringExtra("current_node_name");
        //是否签收
        is_sign = getIntent().getBooleanExtra("is_sign", false);
        from_id = getIntent().getStringExtra("from_id");
        data_id = getIntent().getStringExtra("data_id");


        //先展示缓存能展示的数据
        titleTv1.setText("审批办理");
        name_tv.setText("审批人: " + SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));
        sbsxTv.setText(sbsx);
        current_node_name_tv.setText(current_node_name);
        sqrTv.setText("申请人: " + sqr);
        sbsjTv.setText("申报时间: " + sbsj);
        blsxTv.setText(blsx);
    }

    @Override
    public void initData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("business_object_code", business_object_code);
        jsonObject.put("business_object_type", business_object_type);
        jsonObject.put("cid", cid);
        jsonObject.put("current_node_id", current_node_id);
        jsonObject.put("item_id", item_id);
        if (!TextUtils.isEmpty(bsnum)) {
            jsonObject.put("bsnum", bsnum);
        }
        Log.d("PtblXqDbActivityss", jsonObject.toString());
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/piece_show", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                ptblXqBean = JSON.parseObject(str, PtblXqBean.class);
                if (lxdhTv == null){
                    return;
                }
                if (ptblXqBean.getNextStepInfo().size() != 0) {
                    nodeId = ptblXqBean.getNextStepInfo().get(0).getNodeId();
                    nodeName = ptblXqBean.getNextStepInfo().get(0).getNodeName();
                    condition = ptblXqBean.getNextStepInfo().get(0).getCondition();
                    gateway = ptblXqBean.getNextStepInfo().get(0).getGateway();
                }


                lxdhTv.setText(ptblXqBean.getLink_phone());
                zjlxTv.setText(ptblXqBean.getIdentity_type());
                zjhmTv.setText(ptblXqBean.getIdcard_no());
                ptblxqAdapter = new PtblxqAdapter();
                blyj_show = ptblXqBean.getBlyj_show();
                ptblxqAdapter.setBlyjShowBeanList(blyj_show);
                rv.setLayoutManager(new LinearLayoutManager(PtblXqDbActivity.this));
                rv.setAdapter(ptblxqAdapter);
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {

            }
        });

    }


    @OnClick({R.id.back_iv, R.id.commit_tv, R.id.zk_ll, R.id.ywsjck_ll, R.id.sbclck_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.commit_tv:
                if (ptblXqBean != null) {
                    Intent intent = new Intent();
                    if (ptblXqBean.getNextStepInfo().size() > 0) {
                        intent.putExtra("str", ptblXqBean.getNextStepInfo().get(0).getNodeName());
                        List<PtblXqBean.NextStepInfoBean.UsersInfoBean> usersInfoList = ptblXqBean.getNextStepInfo().get(0).getUsersInfo();
                        if (usersInfoList == null) {
                            usersInfoList = new ArrayList<>();
                        }
                        intent.putExtra("list", (Serializable) usersInfoList);
                    }
                    intent.setClass(this, PtblClActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.zk_ll:
                if (zk) {
                    //收起
                    rv.setVisibility(View.GONE);
                    zkTv.setText("展开");
                    zkIv.setBackgroundResource(R.drawable.unfold);
                    zk = false;
                } else {
                    //展开
                    rv.setVisibility(View.VISIBLE);
                    ptblxqAdapter = new PtblxqAdapter();
                    ptblxqAdapter.setBlyjShowBeanList(blyj_show);
                    rv.setLayoutManager(new LinearLayoutManager(PtblXqDbActivity.this));
                    rv.setAdapter(ptblxqAdapter);
                    zkTv.setText("收起");
                    zkIv.setBackgroundResource(R.drawable.packup);
                    zk = true;
                }
                break;
            case R.id.ywsjck_ll:
                YwSjCkActivity.goInto(this, blsxTv.getText().toString(),
                        sqrTv.getText().toString(), lxdhTv.getText().toString(),
                        zjlxTv.getText().toString(), zjhmTv.getText().toString());
                break;
            case R.id.sbclck_ll:
                List<SbclckBean> sbclckBeanList = new ArrayList<>();
                if (ptblXqBean == null) {
                    return;
                }
                for (int i = 0; i < ptblXqBean.getMaterial().size(); i++) {
                    sbclckBeanList.add(new SbclckBean(ptblXqBean.getMaterial().get(i).getFILE_NAME(),
                            ptblXqBean.getMaterial().get(i).getFILE_PATH(),
                            ptblXqBean.getMaterial().get(i).getFILE_URL()));
                }
                SbclckActivity.goInto(this, sbclckBeanList);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //处理完成之后的返回回调
    }
}
